package com.xxx.kjds.controller;

import com.xxx.kjds.entity.Menu;
import com.xxx.kjds.service.MenuService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * @function : 分页获取菜单信息
     * @param ：pageNum、pageSize
     * @return : 当前页面的菜单信息
     * */
    @RequestMapping("/selectByPage")
    @ResponseBody
    public List<Menu> selectByPage(HttpServletRequest request) {
        Integer pageNo = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        List<Menu> result = new ArrayList();
        List<Menu> list = this.menuService.getList();

        for(int i = 0; i < list.size(); ++i) {
            if (i >= (pageNo - 1) * pageSize && i < pageNo * pageSize) {
                result.add(list.get(i));
            }
        }

        return result;
    }

    /**
     * @function : 获取某种角色的权限信息
     * @param ：角色类型type
     * @return : 权限信息列表
     * */
    @RequestMapping("/selectMenu2")
    @ResponseBody
    public List<Menu> selectMenu2(HttpServletRequest request) {
        String type = request.getParameter("type");
        byte menu_id;
        if (Integer.parseInt(type) == 1) {
            menu_id = 1;
        } else if (Integer.parseInt(type) == 2) {
            menu_id = 6;
        } else {
            menu_id = 12;
        }

        List<Menu> menuList = this.menuService.getList();
        List<Menu> menuList2 = new ArrayList();
        Iterator var6 = menuList.iterator();

        while(var6.hasNext()) {
            Menu e = (Menu)var6.next();
            if (e.getMenuId() == menu_id) {
                menuList2.add(e);
            }
        }

        return menuList2;
    }

    /**
     * @function : 获取菜单信息
     * @param ：无
     * @return : 菜单信息列表
     * */
    @RequestMapping("/selectMenu")
    @ResponseBody
    public List<Menu> selectMenu() {
        return this.menuService.getList();
    }


    /**
     * @function : 添加菜单
     * @param ：菜单信息
     * @return : 添加菜单成功/添加菜单失败，查看菜单名是否已存在
     * */
    @RequestMapping("/addMenu")
    @ResponseBody
    public String addMenu(HttpServletRequest request) {
        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");
        String url = request.getParameter("url");
        Menu menu = new Menu();
        if (menu_id.equals("1")) {
            name = "品牌商-" + name;
        } else if (menu_id.equals("6")) {
            name = "借卖方-" + name;
        } else {
            name = "管理员-" + name;
        }

        menu.setName(name);
        menu.setMenuId(Integer.parseInt(menu_id));
        menu.setLevel(Integer.parseInt(level));
        menu.setStatus(1);
        menu.setUrl(url);
        int result = this.menuService.insert(menu);
        return result > 0 ? "添加菜单成功" : "添加菜单失败，查看菜单名是否已存在";
    }

    /**
     * @function : 修改菜单
     * @param ：菜单信息
     * @return : 修改成功/修改失败
     * */
    @RequestMapping("/updateMenu")
    @ResponseBody
    public String updateMenu(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");
        String url = request.getParameter("url");
        Menu menu = new Menu();
        menu.setId(Integer.parseInt(id));
        menu.setName(name);
        menu.setMenuId(Integer.parseInt(menu_id));
        menu.setLevel(Integer.parseInt(level));
        menu.setStatus(1);
        menu.setUrl(url);
        return this.menuService.updateByPrimaryKey(menu) == 1 ? "修改成功" : "修改失败";
    }

    /**
     * @function : 删除菜单
     * @param ：id
     * @return : 删除菜单成功/删除菜单失败
     * */
    @RequestMapping("/deteleMenu")
    @ResponseBody
    public String deteleMenu(HttpServletRequest request) {
        String id = request.getParameter("id");
        int result = this.menuService.deleteByPrimaryKey(Integer.parseInt(id));
        return result > 0 ? "删除菜单成功!" : "删除菜单失败!";
    }

    /**
     * @function : 修改用户的权限
     * @param ：封装到权限信息
     * @return : success/error
     * */
    @RequestMapping("/updateMenuList")
    @ResponseBody
    public String updateMenuList(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");
        String status = request.getParameter("status");
        String url = request.getParameter("url");
        Menu menu = new Menu();
        menu.setId(Integer.valueOf(id));
        menu.setName(name);
        menu.setMenuId(Integer.valueOf(menu_id));
        menu.setLevel(Integer.valueOf(level));
        menu.setStatus(Integer.valueOf(status));
        menu.setUrl(url);
        return this.menuService.updateByPrimaryKey(menu) == 1 ? "success" : "error";
    }
}
